package com.maoyan.quickdevelop.controller.dqinterview;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.maoyan.quickdevelop.common.core.domain.DqType;
import com.maoyan.quickdevelop.system.domain.vo.EchartsVO;
import com.maoyan.quickdevelop.common.utils.StringUtils;
import com.maoyan.quickdevelop.common.annotation.Log;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.AjaxResult;
import com.maoyan.quickdevelop.common.core.domain.DqInterview;
import com.maoyan.quickdevelop.common.core.domain.postprocessor.DqInterviewPostProcesser;
import com.maoyan.quickdevelop.common.enums.BusinessType;
import com.maoyan.quickdevelop.common.utils.DateUtils;
import com.maoyan.quickdevelop.system.domain.DqInterviewVO;
import com.maoyan.quickdevelop.system.service.IDqInterviewService;
import com.maoyan.quickdevelop.system.service.IDqTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 猫颜
 * @date 2021/5/29 8:49
 * TODO 文章管理类
 */
@RestController
@RequestMapping("/dqinterview")
public class DqInterviewController {
    @Autowired
    private IDqInterviewService iInterviewService;
    @Resource
    private IDqTypeService dqTypeService;


    /**
     * TODO 文章通用查询
     *
     * @param pageNum     第几页
     * @param pageSize    每页几个
     * @param dqInterview 查询信息
     * @return com.maoyan.quickdevelop.common.core.AjaxResult
     * @author 猫颜
     * @date 上午9:58
     */
    @GetMapping("/list")
    @ApiOperation(value = "通用查询所有的文章")
    public AjaxResult listInterview(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
                                    @RequestParam(defaultValue = "10", name = "pageSize") int pageSize,
                                    DqInterview dqInterview) {
        List<DqInterview> dqInterviews = iInterviewService.selectDqInterview(pageNum, pageSize, dqInterview);
        return AjaxResult.success("查询成功", new PageInfo<>(dqInterviews));

    }

    /**
     * 根据文章ID查询文章
     *
     * @param dqInterviewId
     * @return com.maoyan.quickdevelop.common.core.AjaxResult
     * @author 猫颜
     * @date 上午8:55
     */
    @GetMapping("/{dqInterviewId}")
    @ApiOperation(value = "根据文章ID查询文章")
    public AjaxResult getDqInterviewById(@PathVariable Long dqInterviewId) {
        DqInterview dqInterview = iInterviewService.selectDqInterviewById(dqInterviewId);
        return AjaxResult.success("查询成功", dqInterview);
    }

    /**
     * 根据标题查询文章
     *
     * @param interviewTitle
     * @return
     */
    @GetMapping("/title")
    @ApiOperation(value = "根据标题查询文章")
    public AjaxResult getByTitle(String interviewTitle) {
        DqInterview dqInterview = iInterviewService.selectDqInterviewByTitle(interviewTitle);
        //Assert.notNull(dqInterview, "没有此标题");
        return AjaxResult.success("查询成功", dqInterview);
    }

    /**
     * 根据作者Id查询文章
     *
     * @param pageNum
     * @param pageSize
     * @param authorId
     * @return
     */
    @GetMapping("/author/{authorId}")
    @ApiOperation(value = "根据作者的ID查询文章")
    public AjaxResult getByAuthorId(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize,
                                    @PathVariable Long authorId) {
        List<DqInterview> dqInterviews = iInterviewService.selectDqInterviewsByAuthorId(pageNum, pageSize, authorId);
        if (StringUtils.isNull(dqInterviews) || dqInterviews.size() == 0) {
            return AjaxResult.error(HttpStatus.NOT_FOUND, "此用户没有文章");
        }
        return AjaxResult.success("查询成功", new PageInfo<>(dqInterviews));
    }

    /**
     * 通过作者昵称查询(昵称重复问题，我暂时懒得解决，对，我懒)(此处已经换成了根据作者的用户名，不再是昵称了)
     *
     * @param pageNum
     * @param pageSize
     * @param authorUsername
     * @return
     */
    @GetMapping("/author")
    @ApiOperation(value = "根据作者的用户名查询文章")
    public AjaxResult getByAuthorUserName(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          String authorUsername) {
        List<DqInterview> dqInterviews = iInterviewService.selectDqInterviewsByAuthorUsername(pageNum, pageSize, authorUsername);
        if (StringUtils.isNull(dqInterviews) || dqInterviews.size() == 0) {
            return AjaxResult.error(HttpStatus.NOT_FOUND, "此用户没有文章");
        }
        return AjaxResult.success("查询成功", new PageInfo<>(dqInterviews));
    }


    /**
     * 根据Id删除文章
     *
     * @param interviewId
     * @return
     */
    @GetMapping("/delete/{interviewId}")
    @ApiOperation(value = "根据ID删除文章")
    public AjaxResult deleteByInterviewId(@PathVariable Long interviewId) {
//        文章作者ID
        int i = iInterviewService.deleteDqInterviewById(interviewId);
        return AjaxResult.success("删除成功", i);
    }

    /***
     * @Description: 发表文章
     * @DateTime: 上午11:37 2021/7/30
     * @Params:
     * @param dqInterviewVO
     * @Return
     * @return com.maoyan.quickdevelop.common.core.AjaxResult
     */
    @PostMapping("/add")
    @Log(title = "文章操作", businessType = BusinessType.INSERT)
    @ApiOperation(value = "发表文章")
    public AjaxResult addInterview(@RequestBody DqInterviewVO dqInterviewVO) {
        DqInterview dqInterview = new DqInterview();
        dqInterview.setInterTitle(dqInterviewVO.getInterTitle());
        dqInterview.setInterInfo(dqInterviewVO.getInterInfo());
        dqInterview.setInterPhone(dqInterviewVO.getInterPhone());
        dqInterview.setInterCategory(dqInterviewVO.getInterCategory());
        dqInterview.setInterTechnology(dqInterviewVO.getInterTechnology());
        dqInterview.setInterAddress(dqInterviewVO.getInterAddress());
        dqInterview.setInterMoney(dqInterviewVO.getInterMoney());
        dqInterview.setInterType(dqInterviewVO.getInterType());
        dqInterview.setInterImage(dqInterviewVO.getInterImage());
        dqInterview.setAuthorId(StpUtil.getLoginIdAsLong());
        dqInterview.setCreateTime(DateUtils.getNowDate());
        dqInterview.setUpdateTime(DateUtils.getNowDate());
        dqInterview.setStatus("0");
        int i = iInterviewService.insertDqInterview(dqInterview);
        return AjaxResult.success("添加成功", i);
    }

    /**
     * 更新文章
     *
     * @param dqInterview
     * @return com.maoyan.quickdevelop.common.core.AjaxResult
     * @author 猫颜
     * @date 上午10:23
     */
    @ApiOperation(value = "更新文章")
    @Log(title = "文章操作", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateInterview(@RequestBody DqInterview dqInterview) {
        int i = iInterviewService.updateDqInterview(dqInterview);
        return AjaxResult.success("更新成功", i);
    }

    /**
     * ========================强化查询===============================
     **/
    @GetMapping("/superlist")
    @ApiOperation(value = "通用查询所有的文章")
    public AjaxResult selectDqInterviewPostProcesser(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
                                                     @RequestParam(defaultValue = "10", name = "pageSize") int pageSize,
                                                     DqInterviewPostProcesser dqInterviewPostProcesser) {
        //参数接到了
//        System.out.println("页数为："+pageNum);
//        System.out.println("每页为："+pageSize);
        List<DqInterviewPostProcesser> dqInterviewPostProcessers = iInterviewService.selectDqInterviewPostProcessers(pageNum, pageSize, dqInterviewPostProcesser);
        return AjaxResult.success("查询成功", new PageInfo<>(dqInterviewPostProcessers));
    }


    @RequestMapping("/listByType")
    public AjaxResult listByType() {
        List<DqType> list = dqTypeService.selectDqTypeByPid(3);
        List<EchartsVO> echartsVOList = new ArrayList<>();
        for (DqType type : list) {
            EchartsVO e = new EchartsVO();
            e.setName(type.getTypeName());
            e.setValue(iInterviewService.selectDqInterviewByType(type.getTypeId()));
            echartsVOList.add(e);
        }
        return AjaxResult.success(echartsVOList);
    }

    @RequestMapping("/listByMoney")
    public AjaxResult listByMoney() {
        List<EchartsVO> echartsVOList = new ArrayList<>();
        EchartsVO e = new EchartsVO();
        e.setName("0-5000");
        e.setValue(iInterviewService.selectDqInterviewByMoney(0, 5000));
        echartsVOList.add(e);
        EchartsVO e1 = new EchartsVO();
        e1.setName("5000-10000");
        e1.setValue(iInterviewService.selectDqInterviewByMoney(5000, 10000));
        echartsVOList.add(e1);
        EchartsVO e2 = new EchartsVO();
        e2.setName("10000-15000");
        e2.setValue(iInterviewService.selectDqInterviewByMoney(10000, 15000));
        echartsVOList.add(e2);
        EchartsVO e3 = new EchartsVO();
        e3.setName("15000+");
        e3.setValue(iInterviewService.selectDqInterviewByMoney(15000, -1));
        echartsVOList.add(e3);
        return AjaxResult.success(echartsVOList);
    }

    @RequestMapping("/listByAddress")
    public AjaxResult listByAddress() {
        List<String> list = iInterviewService.selectDqInterviewAddress();
        List<EchartsVO> echartsVOList = new ArrayList<>();
        for (String s : list) {
            EchartsVO e = new EchartsVO();
            e.setName(s);
            e.setValue(iInterviewService.selectDqInterviewByAddress(s));
            echartsVOList.add(e);
        }
        return AjaxResult.success(echartsVOList);
    }

}
